package com.peng.test.backoff;

import java.util.Random;

/**
 * RandomBackOff
 * 随机指数退避算法
 *
 * @author: lupeng6
 * @create: 2020/12/10 10:28
 */
public class RandomBackOff {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Solution().retry(i);
        }
    }


    static class Solution {
        public void retry(int n) {
            int maxBorder = 1 << n;
            System.out.println(String.format("第%s次重试, 最大值%s", n, maxBorder));
            Random random = new Random();
            int i = random.nextInt(maxBorder);
            System.out.println(String.format("睡眠时间%s", i));
            try {
                Thread.sleep(i * 1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
